package com.turing.post.organization.impl;

import java.math.BigInteger;
import java.util.Date;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import com.turing.post.cardmanagement.util.ActivateCard;
import com.turing.post.cardmanagement.util.ActivateCardDao;
import com.turing.post.cardmanagement.util.CardStock;
import com.turing.post.cardmanagement.util.CardStockDao;
import com.turing.post.cardmanagement.util.SalesGetCardOrder;
import com.turing.post.cardmanagement.util.SalesGetCardOrderDao;
import com.turing.post.common.util.PCPSCommonDao;
import com.turing.post.common.util.SysOperLog;
import com.turing.post.common.util.SysOperLogDao;
import com.turing.post.organization.util.IssueCompany;
import com.turing.post.organization.util.IssueCompanyDao;
import com.turing.post.organization.util.IssueFeePolicy;
import com.turing.post.organization.util.IssueFeePolicyBusiness;
import com.turing.post.organization.util.IssueFeePolicyDao;
import com.turing.post.organization.util.OrganApplicationException;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

/**
 * @author zxp
 * @Mar 12, 2013
 */
@Stateless(mappedName = "organization.impl.IssueFeePolicyBusinessImpl")
@Remote
@Local
public class IssueFeePolicyBusinessImpl implements IssueFeePolicyBusiness {

	@EJB(mappedName = "common.impl.SysOperLogDaoImpl")
	private SysOperLogDao sysOperLogDaoImpl;

	@EJB(mappedName = "	common.impl.PCPSCommonDaoImpl")
	private PCPSCommonDao commonDaoImpl;

	@EJB(mappedName = "organization.impl.IssueCompanyDaoImpl")
	private IssueCompanyDao issueCompanyDaoImpl;

	@EJB(mappedName = "	organization.impl.IssueFeePolicyDaoImpl")
	private IssueFeePolicyDao issueFeePolicyDaoImpl;

	@EJB(mappedName = "cardmanagement.impl.ActivateCardDaoImpl")
	private ActivateCardDao activateCardDaoImpl;

	@EJB(mappedName = "cardmanagement.impl.CardStockDaoImpl")
	private CardStockDao cardStockDao;

	@EJB(mappedName = "common.impl.PCPSCommonDaoImpl")
	private PCPSCommonDao pcpsCommonDaoImpl;

	@Override
	public void authIssueFeePolicy(PCPSWebInput input,
			IssueFeePolicy issueFeePolicy) throws PCPSApplicationException {
		if (input == null || PCPSUtil.isEmpty(input.getOperBranchNo())) {
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"用户归属机构号为空!");
		}
		if (issueFeePolicy == null || issueFeePolicy.getFeePolicyId() == null) {
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"待审核资费标准对象为空!");
		}
		IssueFeePolicy _issueFeePolicy = issueFeePolicyDaoImpl
				.getIssueFeePolicyByFeePolicyId(issueFeePolicy.getFeePolicyId());
		if (_issueFeePolicy == null) {
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"待审核资费标准系统不存在!");
		}
		if (_issueFeePolicy.getAuthOperId() == null) {
			if (_issueFeePolicy.getCreateOperId().compareTo(input.getOperId())==0) {
				throw new OrganApplicationException(
						OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
						"", "审核人不可为创建人!");
			}
		} else {
			if (_issueFeePolicy.getAuthOperId().compareTo(input.getOperId())==0) {
				throw new OrganApplicationException(
						OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
						"", "审核人不可为申请人!");
			}
		}
		_issueFeePolicy.setStatus(issueFeePolicy.getStatus());
		_issueFeePolicy.setAuthOperId(input.getOperId());
		_issueFeePolicy.setAuthTime(commonDaoImpl.getDatabaseSysTime());
		issueFeePolicyDaoImpl.update(_issueFeePolicy);
		// 日志记录
		sysOperLogDaoImpl.insertSysOperLogInfo(input, SysOperLog.operType_Edit,
				"审核发卡商资费策略", "审核发卡商资费策略， 策略名称："
						+ _issueFeePolicy.getFeePolicyName());
	}

	@Override
	public void deleteIssueFeePolicy(PCPSWebInput input,
			IssueFeePolicy issueFeePolicy) throws PCPSApplicationException {
		if (issueFeePolicy == null || issueFeePolicy.getFeePolicyId() == null) {
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"待删除资费标准对象为空!");
		}
		IssueFeePolicy _issueFeePolicy = issueFeePolicyDaoImpl
				.getIssueFeePolicyByFeePolicyId(issueFeePolicy.getFeePolicyId());
		if (_issueFeePolicy == null) {
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"待删除资费标准系统不存在!");
		}
		if (!IssueFeePolicy.IFPolicy_status_EffectiveWait
				.equals(_issueFeePolicy.getStatus())) {
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"只可删除无效资费标准!");
		}
		// 日志记录
		sysOperLogDaoImpl.insertSysOperLogInfo(input, SysOperLog.operType_Edit,
				"删除发卡商资费策略", "删除发卡商资费策略， 策略名称："
						+ _issueFeePolicy.getFeePolicyName());
		issueFeePolicyDaoImpl.delete(_issueFeePolicy.getFeePolicyId());
	}

	@Override
	public void insertIssueFeePolicy(PCPSWebInput input,
			IssueFeePolicy issueFeePolicy) throws PCPSApplicationException {
		if (issueFeePolicy == null || issueFeePolicy.getFeePolicyName() == null) {
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"资费标准对象为空!");
		}
		if (PCPSUtil.isEmpty(issueFeePolicy.getFeePolicyName())
				|| PCPSUtil.isEmpty(issueFeePolicy.getBusinessType())) {
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"资费标准必输项为空!");
		}
		String issueCompanyNo = input.getOperBranchNo().substring(0, 6);
		issueFeePolicy.setIssueCompanyNo(issueCompanyNo);
		issueFeePolicy.setStatus(IssueFeePolicy.IFPolicy_status_EffectiveWait);
		issueFeePolicy.setCreateOperId(input.getOperId());
		issueFeePolicy.setCreateTime(commonDaoImpl.getDatabaseSysTime());
		issueFeePolicy.setFeePolicyId(commonDaoImpl.getUniqueIntegerKey());

		Integer ifpCount = issueFeePolicyDaoImpl
				.getIssueFeePolicyCountByIFP(issueFeePolicy);
		// 判断与该对象执行条件相同的总数是否大于0
		if (ifpCount > 0) {
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"已存在与该资费执行条件相同的记录，请先将旧资费设置为无效，再继续添加该资费策略！");
		}
		issueFeePolicyDaoImpl.insert(issueFeePolicy);
		// 日志记录
		sysOperLogDaoImpl.insertSysOperLogInfo(input, SysOperLog.operType_Edit,
				"添加发卡商资费策略", "添加发卡商资费策略， 策略名称："
						+ issueFeePolicy.getFeePolicyName());
	}

	@Override
	public void updateIssueFeePolicy(PCPSWebInput input,
			IssueFeePolicy issueFeePolicy) throws PCPSApplicationException {
		if (issueFeePolicy == null || issueFeePolicy.getFeePolicyId() == null) {
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"待修改资费标准对象为空!");
		}
		IssueFeePolicy _issueFeePolicy = issueFeePolicyDaoImpl
				.getIssueFeePolicyByFeePolicyId(issueFeePolicy.getFeePolicyId());
		if (_issueFeePolicy == null) {
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"待修改资费标准系统不存在!");
		}
		if (IssueFeePolicy.IFPolicy_status_InvalidWait.equals(_issueFeePolicy
				.getStatus())
				|| IssueFeePolicy.IFPolicy_status_Effective
						.equals(_issueFeePolicy.getStatus())) {
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"只可修改待审资费标准!");
		}
		_issueFeePolicy.setSaleBranchNo(issueFeePolicy.getSaleBranchNo());
		_issueFeePolicy.setFeePolicyName(issueFeePolicy.getFeePolicyName());
		_issueFeePolicy.setBusinessType(issueFeePolicy.getBusinessType());
		_issueFeePolicy.setCardFaceId(issueFeePolicy.getCardFaceId());
		_issueFeePolicy.setCardClassicId(issueFeePolicy.getCardClassicId());
		_issueFeePolicy.setToCardClassicId(issueFeePolicy.getToCardClassicId());
		_issueFeePolicy.setStartCardSeqNo(issueFeePolicy.getStartCardSeqNo());
		_issueFeePolicy.setEndCardSeqNo(issueFeePolicy.getEndCardSeqNo());
		_issueFeePolicy.setStartAmount(issueFeePolicy.getStartAmount());
		_issueFeePolicy.setEndAmount(issueFeePolicy.getEndAmount());
		_issueFeePolicy.setFeeType(issueFeePolicy.getFeeType());
		_issueFeePolicy.setFixFee(issueFeePolicy.getFixFee());
		_issueFeePolicy.setFixRatio(issueFeePolicy.getFixRatio());
		_issueFeePolicy.setAmountType(issueFeePolicy.getAmountType());
		_issueFeePolicy.setFeeCycle(issueFeePolicy.getFeeCycle());
		_issueFeePolicy.setRemark(issueFeePolicy.getRemark());
		_issueFeePolicy.setStartDate(issueFeePolicy.getStartDate());
		_issueFeePolicy.setEndDate(issueFeePolicy.getEndDate());
		_issueFeePolicy.setStatus(IssueFeePolicy.IFPolicy_status_EffectiveWait);
		issueFeePolicyDaoImpl.update(_issueFeePolicy);
		// 日志记录
		sysOperLogDaoImpl.insertSysOperLogInfo(input, SysOperLog.operType_Edit,
				"修改发卡商资费策略", "修改发卡商资费策略， 策略名称："
						+ _issueFeePolicy.getFeePolicyName());
	}

	/**
	 * 计算发卡商业务资费
	 * 
	 * @param issueCompanyNo
	 *            发卡商号
	 * @param saleBranchNo
	 *            销售机构号(可选)
	 * @param businessType
	 *            业务类型
	 * @param cardNo
	 *            卡号
	 * @param toCardClassicId
	 *            目标卡种类(仅变更卡种类需要)
	 * @param tranDate
	 *            日期
	 * @param amount
	 *            发生额或余额
	 * @return
	 */
	public IssueFeePolicy calcCardHolderFee(String issueCompanyNo,
			String saleBranchNo, String businessType, String cardNo,
			BigInteger toCardClassicId, Date tranDate, BigInteger amount)
			throws PCPSApplicationException {
		ActivateCard ac = activateCardDaoImpl.getActivateCardByCardNo(cardNo);
		if (ac == null)
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"卡号无效!");

		return calculateCardHolderFee(issueCompanyNo, saleBranchNo,
				businessType, cardNo, ac.getCardSeqNo(), ac.getCardFaceId(), ac
						.getCardClassicId(), toCardClassicId, tranDate, amount,
				ac.getBalance());
	}

	/**
	 * 计算发卡商业务资费
	 * 
	 * @param issueCompanyNo
	 *            发卡商号
	 * @param saleBranchNo
	 *            销售机构号(可选)
	 * @param businessType
	 *            业务类型
	 * @param cardNo
	 *            卡号
	 * @param cardSeqNo
	 *            卡序号
	 * @param cardFaceId
	 *            卡面ID
	 * @param cardClassicId
	 *            卡种类
	 * @param toCardClassicId
	 *            目标卡种类(仅变更卡种类需要)
	 * @param tranDate
	 *            日期
	 * @param amount
	 *            发生额或余额
	 * @param balance
	 *            卡余额
	 * @return
	 */
	public IssueFeePolicy calculateCardHolderFee(String issueCompanyNo,
			String saleBranchNo, String businessType, String cardNo,
			String cardSeqNo, BigInteger cardFaceId, BigInteger cardClassicId,
			BigInteger toCardClassicId, Date tranDate, BigInteger amount,
			BigInteger balance) throws PCPSApplicationException {
		// System.out.println("issueCompanyNo" + issueCompanyNo + "saleBranchNo"
		// + saleBranchNo + "businessType" + businessType + "cardNo"
		// + cardNo + "cardSeqNo" + cardSeqNo + "cardFaceId" + cardFaceId
		// + "cardClassicId" + cardClassicId + "toCardClassicId"
		// + toCardClassicId + "tranDate" + tranDate + "amount" + amount
		// + "balance" + balance);
		IssueFeePolicy mcf = new IssueFeePolicy();
		mcf.setFixRatio(0);
		mcf.setFee(BigInteger.ZERO);
		int result = IssueFeePolicy.isBalance(businessType);
		if (result < 0)
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"传入的业务类型非法!");

		// 与余额相关
		if (result == 1)
			amount = balance;

		boolean ischangecardclassic = PCPSUtil.compareObject(businessType,
				IssueFeePolicy.IFPolicy_busiType_changeClassic);
		if (ischangecardclassic) {
			if (toCardClassicId == null)
				throw new OrganApplicationException(
						OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
						"", "变更卡种类目标不明确!");
		}

		mcf.setIssueCompanyNo(issueCompanyNo);
		mcf.setSaleBranchNo(saleBranchNo);
		mcf.setBusinessType(businessType);
		mcf.setCardFaceId(cardFaceId);
		mcf.setCardClassicId(cardClassicId);
		mcf.setToCardClassicId(toCardClassicId);
		mcf.setStartCardSeqNo(cardSeqNo);
		mcf.setStartDate(tranDate);
		mcf.setStartAmount(amount);

		List<IssueFeePolicy> mcflist = issueFeePolicyDaoImpl
				.getIssueFeePolicyByBusiness(mcf);
		if (mcflist == null || mcflist.size() < 1)
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"业务资费策略未定义!");

		IssueFeePolicy curmfp = null;
		int curmfpscore = 0;
		int score = 0;

		// 评估策略的优先级,匹配多者优先
		for (IssueFeePolicy lmfp : mcflist) {
			score = 0;

			// 变更卡种类
			if (ischangecardclassic) {
				// 若源种类非空,则必须与源种类一致
				if (lmfp.getCardClassicId() != null) {
					if (!PCPSUtil.compareObject(lmfp.getCardClassicId(),
							cardClassicId))
						continue;
				}
				// 若目标种类非空,则必须与目标种类一致
				if (lmfp.getToCardClassicId() != null) {
					if (!PCPSUtil.compareObject(lmfp.getToCardClassicId(),
							toCardClassicId))
						continue;
				}
			}

			// 销售分支机构匹配上+1
			if (PCPSUtil.compareObject(lmfp.getSaleBranchNo(), saleBranchNo))
				score++;

			// 业务类型匹配上+1
			if (PCPSUtil.compareObject(lmfp.getBusinessType(), businessType))
				score++;

			// 卡面匹配上+1
			if (PCPSUtil.compareObject(lmfp.getCardFaceId(), cardFaceId))
				score++;

			// 卡种类匹配上+1
			if (PCPSUtil.compareObject(lmfp.getCardClassicId(), cardClassicId))
				score++;

			// 目标卡种类匹配上+1
			if (PCPSUtil.compareObject(lmfp.getToCardClassicId(),
					toCardClassicId))
				score++;

			// 卡序号范围完全匹配上+3,匹配上1个+1
			if (!PCPSUtil.isEmpty(lmfp.getStartCardSeqNo())
					&& !PCPSUtil.isEmpty(lmfp.getEndCardSeqNo()))
				score += 3;
			else if (PCPSUtil.isEmpty(lmfp.getStartCardSeqNo())
					&& PCPSUtil.isEmpty(lmfp.getEndCardSeqNo()))
				score += 0;
			else
				score++;

			// 日期范围完全匹配上+3,匹配上1个+1
			if (lmfp.getStartDate() != null && lmfp.getEndDate() != null)
				score += 3;
			else if (lmfp.getStartDate() == null && lmfp.getEndDate() == null)
				score += 0;
			else
				score++;

			// 金额范围完全匹配上+3,匹配上1个+1
			if (BigInteger.ZERO.compareTo(lmfp.getStartAmount()) < 0
					&& BigInteger.ZERO.compareTo(lmfp.getEndAmount()) < 0)
				score += 3;
			else if (BigInteger.ZERO.compareTo(lmfp.getStartAmount()) >= 0
					&& BigInteger.ZERO.compareTo(lmfp.getEndAmount()) >= 0)
				score += 0;
			else
				score++;

			if (score > curmfpscore || curmfp == null) {
				curmfp = lmfp;
				curmfpscore = score;
			}
		}

		if (curmfp == null) {
			if (ischangecardclassic)
				throw new OrganApplicationException(
						OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
						"", "变更卡种类资费策略未定义!");
			else
				throw new OrganApplicationException(
						OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
						"", "业务资费策略未定义!");
		}

		// 固定费率
		if (PCPSUtil.compareObject(curmfp.getFeeType(),
				IssueFeePolicy.IFPolicy_FeeType_Fix))
			curmfp.setFee(BigInteger.valueOf(curmfp.getFixFee()));
		else if (PCPSUtil.compareObject(curmfp.getFeeType(),
				IssueFeePolicy.IFPolicy_FeeType_Ratio)) {
			// curmfp.setFee(BigInteger.valueOf(curmfp.getFixRatio()).multiply(
			// amount).divide(BigInteger.valueOf(10000)));
			curmfp.setFee(PCPSUtil.calcFee(BigInteger.valueOf(curmfp
					.getFixRatio()), amount));
		}

		if (BigInteger.ZERO.compareTo(curmfp.getFee()) > 0) {
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"根据资费策略计算出的手续费非法!");
		}

		return curmfp;
	}

	@Override
	public IssueFeePolicy calcSaleOrderFee(List<IssueFeePolicy> list)
			throws PCPSApplicationException {
		if (list == null) {
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"参数对象为空!");
		}
		IssueCompany ic = issueCompanyDaoImpl
				.getIssueCompanyByIssueCompanyNo(list.get(0)
						.getIssueCompanyNo());
		BigInteger fee = BigInteger.ZERO;
		for (IssueFeePolicy vo : list) {
			IssueFeePolicy issueFeePolicy = calculateCardHolderFee(vo
					.getIssueCompanyNo(), vo.getSaleBranchNo(), vo
					.getBusinessType(), null, vo.getStartCardSeqNo(), vo
					.getCardFaceId(), vo.getCardClassicId(), null, ic
					.getTranDate(), vo.getStartAmount(), null);
			if (issueFeePolicy == null || issueFeePolicy.getFee() == null) {
				throw new OrganApplicationException(
						OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
						"", "手续费计算失败!");
			}
			fee = fee.add(issueFeePolicy.getFee());
		}
		IssueFeePolicy resultvo = new IssueFeePolicy();
		resultvo.setFee(fee);
		return resultvo;
	}

	@EJB(mappedName = "cardmanagement.impl.SalesGetCardOrderDaoImpl")
	private SalesGetCardOrderDao sgcoDaoImpl;

	@Override
	public IssueFeePolicy calcSaleCardFee(PCPSWebInput webInput,
			String saleBranchNo, String businessType, String startSeqNo,
			String endSeqNo, BigInteger cardFaceId, BigInteger cardClassicId,
			BigInteger cardParvalueId, String salesGetCardOrderNo)
			throws PCPSApplicationException {
		if (webInput == null || PCPSUtil.isEmpty(webInput.getOperBranchNo())
				|| PCPSUtil.isEmpty(saleBranchNo)
				|| PCPSUtil.isEmpty(businessType)
				|| PCPSUtil.isEmpty(startSeqNo) || PCPSUtil.isEmpty(endSeqNo)
				|| cardFaceId == null || cardClassicId == null
				|| cardParvalueId == null) {
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"参数对象为空!");
		}
		String cardStockStatus = CardStock.CARDSTOCK_STATUS_NORMAL;
		// 营销员领卡单号
		if (!PCPSUtil.isEmpty(salesGetCardOrderNo)) {
			SalesGetCardOrder sgco = sgcoDaoImpl
					.getSalesGetCardOrderBySalesGetCardOrderNo(salesGetCardOrderNo);
			if (sgco == null) {
				throw new OrganApplicationException(
						OrganApplicationException.ERRMSG_NULLPOINTER, "",
						"营销员领卡单号" + salesGetCardOrderNo + "系统中不存在!");
			}
			cardStockStatus = CardStock.CARDSTOCK_STATUS_INTRANSIT;
		}

		List<CardStock> list = cardStockDao
				.getCardStockListBySaleBranchNoAndStatusAndCredseqno(
						startSeqNo, endSeqNo, cardStockStatus, saleBranchNo);
		if (list == null || list.size() <= 0) {
			throw new OrganApplicationException(
					OrganApplicationException.ERRMSG_NULLPOINTER, "",
					"售卡信息不存在或卡信息有误!");
		}

		Date curTime = pcpsCommonDaoImpl.getDatabaseSysTime();
		IssueFeePolicy resultvo = new IssueFeePolicy();
		resultvo.setFee(BigInteger.ZERO);
		String issueCompanyNo = PCPSUtil.getIssueCompanyNo(webInput);
		boolean check = checkCardSeqNoFeePolicy(issueCompanyNo, saleBranchNo,
				businessType, null, null, cardFaceId, cardClassicId, null,
				curTime, cardParvalueId, null);

		for (CardStock vo : list) {
			IssueFeePolicy issueFeePolicy = calculateCardHolderFee(
					issueCompanyNo, saleBranchNo, businessType, null, vo
							.getCardSeqNo(), cardFaceId, cardClassicId, null,
					null, cardParvalueId, null);

			if (issueFeePolicy == null || issueFeePolicy.getFee() == null) {
				throw new OrganApplicationException(
						OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
						"", "手续费计算失败!");
			}
			if (check) {
				BigInteger size = new BigInteger(String.valueOf(list.size()));
				resultvo.setFee(issueFeePolicy.getFee().multiply(size));
			} else {
				resultvo.setFee(resultvo.getFee().add(issueFeePolicy.getFee()));
			}
		}
		return resultvo;
	}

	@Override
	public IssueFeePolicy calcDepositCardFee(PCPSWebInput webInput,
			String saleBranchNo, String businessType,
			List<IssueFeePolicy> dodList) throws PCPSApplicationException {
		if (webInput == null || PCPSUtil.isEmpty(webInput.getOperBranchNo())
				|| PCPSUtil.isEmpty(saleBranchNo)
				|| PCPSUtil.isEmpty(businessType)) {
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"参数对象为空!");
		}

		if (dodList == null || dodList.size() <= 0) {
			throw new OrganApplicationException(
					OrganApplicationException.ERRMSG_NULLPOINTER, "",
					"充值卡信息为空,导入充值卡信息!");
		}
		IssueFeePolicy resultvo = new IssueFeePolicy();
		resultvo.setFee(BigInteger.ZERO);
		resultvo.setStartAmount(BigInteger.ZERO);
		for (IssueFeePolicy vo : dodList) {
			if (PCPSUtil.isEmpty(vo.getStartCardSeqNo())) {
				throw new OrganApplicationException(
						OrganApplicationException.ERRMSG_NULLPOINTER, "",
						"批量充值卡顺序号为空!");
			}
			if (vo.getStartAmount().compareTo(BigInteger.ZERO) <= 0) {
				throw new OrganApplicationException(
						OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
						"", "批量充值金额必须大于0:" + vo.getStartCardSeqNo());
			}
			ActivateCard activateCard = activateCardDaoImpl
					.getActivateCardByCardSeqNo(vo.getStartCardSeqNo());
			if (activateCard == null) {
				throw new OrganApplicationException(
						OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
						"", "卡序号" + vo.getStartCardSeqNo() + "不存在!");
			}
			if (!vo.getStartCardSeqNo().equals(activateCard.getCardSeqNo())) {
				throw new OrganApplicationException(
						OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
						"", "充值卡序号" + vo.getStartCardSeqNo() + "与激活卡序号"
								+ activateCard.getCardSeqNo() + "不对应!");
			}
			String issueCompanyNo = PCPSUtil.getIssueCompanyNo(webInput);
			IssueFeePolicy issueFeePolicy = calculateCardHolderFee(
					issueCompanyNo, saleBranchNo, businessType, null,
					activateCard.getCardSeqNo(), activateCard.getCardFaceId(),
					activateCard.getCardClassicId(), null, null, vo
							.getStartAmount(), null);
			if (issueFeePolicy == null || issueFeePolicy.getFee() == null) {
				throw new OrganApplicationException(
						OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
						"", "手续费计算失败!");
			}
			resultvo.setFee(resultvo.getFee().add(issueFeePolicy.getFee()));
			resultvo.setStartAmount(resultvo.getStartAmount().add(
					vo.getStartAmount()));
		}
		return resultvo;
	}

	/**
	 * 判断是否有按卡号段收费策略
	 * 
	 * @param issueCompanyNo
	 * @param saleBranchNo
	 * @param businessType
	 * @param cardNo
	 * @param cardSeqNo
	 * @param cardFaceId
	 * @param cardClassicId
	 * @param toCardClassicId
	 * @param tranDate
	 * @param amount
	 * @param balance
	 * @return
	 * @throws PCPSApplicationException
	 */
	public boolean checkCardSeqNoFeePolicy(String issueCompanyNo,
			String saleBranchNo, String businessType, String cardNo,
			String cardSeqNo, BigInteger cardFaceId, BigInteger cardClassicId,
			BigInteger toCardClassicId, Date tranDate, BigInteger amount,
			BigInteger balance) throws PCPSApplicationException {
		IssueFeePolicy mcf = new IssueFeePolicy();
		mcf.setFixRatio(0);
		mcf.setFee(BigInteger.ZERO);

		int result = IssueFeePolicy.isBalance(businessType);
		if (result < 0)
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"传入的业务类型非法!");

		// 与余额相关
		if (result == 1)
			amount = balance;

		boolean ischangecardclassic = PCPSUtil.compareObject(businessType,
				IssueFeePolicy.IFPolicy_busiType_changeClassic);
		if (ischangecardclassic) {
			if (toCardClassicId == null)
				throw new OrganApplicationException(
						OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
						"", "变更卡种类目标不明确!");
		}

		mcf.setIssueCompanyNo(issueCompanyNo);
		mcf.setSaleBranchNo(saleBranchNo);
		mcf.setBusinessType(businessType);
		mcf.setCardFaceId(cardFaceId);
		mcf.setCardClassicId(cardClassicId);
		mcf.setToCardClassicId(toCardClassicId);
		mcf.setStartCardSeqNo(cardSeqNo);
		mcf.setStartDate(tranDate);
		mcf.setStartAmount(amount);

		List<IssueFeePolicy> mcflist = issueFeePolicyDaoImpl
				.getIssueFeePolicyByBusiness(mcf);
		if (mcflist == null || mcflist.size() < 1)
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"业务资费策略未定义!");
		boolean check = true;
		for (IssueFeePolicy vo : mcflist) {
			if (!PCPSUtil.isEmpty(vo.getStartCardSeqNo())
					|| !PCPSUtil.isEmpty(vo.getEndCardSeqNo())) {
				check = false;
				break;
			}
		}
		return check;
	}
}
